using System;
using System.Configuration;
using System.Web.Configuration;
using System.Xml;
//using Nop.Configuration.Infrastructure;

namespace Nop.Configuration
{
    /// <summary>
    /// Represents a NopConfig
    /// </summary>
    public partial class NopConfigs : ConfigurationSection, IConfigurationSectionHandler
    {
        private static System.Configuration.Configuration _config;
        private static NopConfigs nopCfgs;

        public static NopConfigs Instance
        {
            get
            {
                if (nopCfgs == null)
                {
                    nopCfgs = CurrentConfiguration.GetSection("NopConfigs") as NopConfigs;
                }
                return nopCfgs;
            }
        }

        public static System.Configuration.Configuration CurrentConfiguration
        {
            get
            {
                if (_config == null)
                {
                    CurrentConfiguration = GetConfigurationFromFile();
                }
                return _config;
            }
            set
            {
                if (_config != value)
                {
                    _config = value;
                    nopCfgs = null;
                }
            }
        }

        internal static System.Configuration.Configuration GetConfigurationFromFile()
        {
            EnsureCurrentConfig();
            return (_config ?? WebConfigurationManager.OpenWebConfiguration("~/"));
            //return (EPiServerFrameworkSection.ConfigurationInstance ?? WebConfigurationManager.OpenWebConfiguration("~/"));
        }


        private static void EnsureCurrentConfig()
        {
            if (_config == null)
            {
                _config = GetConfigurationFromCurrentDomain();
                //ConfigurationInstance = GetConfigurationFromCurrentDomain();
            }
        }


        private static System.Configuration.Configuration GetConfigurationFromCurrentDomain()
        {
            ExeConfigurationFileMap map2 = new ExeConfigurationFileMap();
            map2.ExeConfigFilename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            ExeConfigurationFileMap fileMap = map2;
            return ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        }



        /// <summary>
        /// Creates a configuration section handler.
        /// </summary>
        /// <param name="parent">Parent object.</param>
        /// <param name="configContext">Configuration context object.</param>
        /// <param name="section">Section XML node.</param>
        /// <returns>The created section handler object.</returns>
        public object Create(object parent, object configContext, XmlNode section)
        {
            var config = new NopConfigs();
            var dynamicDiscoveryNode = section.SelectSingleNode("DynamicDiscovery");
            if (dynamicDiscoveryNode != null && dynamicDiscoveryNode.Attributes != null)
            {
                var attribute = dynamicDiscoveryNode.Attributes["Enabled"];
                if (attribute != null)
                    config.DynamicDiscovery = Convert.ToBoolean(attribute.Value);
            }

            var engineNode = section.SelectSingleNode("Engine");
            if (engineNode != null && engineNode.Attributes != null)
            {
                var attribute = engineNode.Attributes["Type"];
                if (attribute != null)
                    config.EngineType = attribute.Value;
            }

            var themeNode = section.SelectSingleNode("Themes");
            if (themeNode != null && themeNode.Attributes != null)
            {
                var attribute = themeNode.Attributes["basePath"];
                if (attribute != null)
                    config.ThemeBasePath = attribute.Value;
            }

            return config;
        }
        
        /// <summary>
        /// In addition to configured assemblies examine and load assemblies in the bin directory.
        /// </summary>
        public bool DynamicDiscovery { get; set; }

        /// <summary>
        /// A custom <see cref="IEngine"/> to manage the application instead of the default.
        /// </summary>
        public string EngineType { get; set; }

        /// <summary>
        /// Specifices where the themes will be stored (~/Themes/)
        /// </summary>
        public string ThemeBasePath { get; set; }
    }
}
